package org.atlantis.grosbeak.plugin;

import org.atlantis.grosbeak.depot.Depot;
import org.atlantis.grosbeak.depot.DepotException;
import org.atlantis.grosbeak.lang.Exceptions;

/**
 * 从IOC容器中获取插件。
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">SeeNoEvil</a>
 * 
 * @param <T>
 */
public class DepotPluginEngine<T> implements PluginEngine<T> {

	private Depot depot;
	private String[] ids;

	public DepotPluginEngine(Depot depot, String... ids) {
		this.depot = depot;
		this.ids = ids;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.atlantis.grosbeak.plugin.PluginEngine#get()
	 */
	@SuppressWarnings("unchecked")
	public T get() throws PluginException {
		for (String id : ids) {
			try {
				Plugin plugin = depot.get(Plugin.class, id);
				if (plugin.canWork()) {
					return (T) plugin;
				}
			} catch (DepotException e) {
			}
		}
		throw Exceptions.makeThrow(PluginException.class,
				"Unable to find any plugins in depot container.");
	}
}
